//
//  RootViewControler.m
//  PriceOnline
//
//  Created by Le Anh Tai on 8/31/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "RootViewControler.h"
#import "AdGADBanner.h"

@interface RootViewControler ()

@end

static RootViewControler *_shared;

@implementation RootViewControler

- (id)init
{
    if (self = [super init])
    {
        _shared = self;
    }
    
    return self;
}

+ (RootViewControler *)shared
{
    return _shared;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
    
    _menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_menuButton addTarget:self action:@selector(menuButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_menuButton];
    
    _menuViewController = [[MenuViewController alloc] init];
    _menuViewController.view.hidden = YES;
    [self.view addSubview:_menuViewController.view];
    
    [self initializeAdsMix];
}

- (void)initializeAdsMix
{
    _adsMix = [[AdsMix alloc] initWithType:AdMixTypeWeight];
    
    AdItem *item2 = [[AdItem alloc] init];
    [item2 configureWithClass:[AdGADBanner class] numberOfAllowFails:1];
    
    [_adsMix configureAds:@[item2]];
    _adsMix.delegate = self;
}

- (void)loadTheme:(BOOL)isDarkTheme
{
    if (isDarkTheme)
    {
        [_menuButton setImage:[UIImage imageNamed:@"icon_menu.png"] forState:UIControlStateNormal];
        
        if (IS_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_IOS7)
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    }
    else
    {
        [_menuButton setImage:[UIImage imageNamed:@"icon_menu.png" color:[UIColor grayColor]] forState:UIControlStateNormal];
        
        if (IS_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_IOS7)
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
    }
}

- (void)didEnterBackground:(id)sender
{
    [_goldViewController saveLastIndexPath];
    [_ratesViewController saveLastIndexPath];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)layoutSubviews
{
    
}

#pragma mark - Delegate
- (void)menuButtonPressed:(UIButton *)sender
{
    [_menuViewController animateOut];
}

#pragma mark -
- (void)adsMix:(AdsMix *)object didLoadAd:(BaseAds *)ad
{
    if (_curAds)
    {
        [_curAds removeFromSuperview];
    }
    
    _curAds = [ad getAds];
    
    [self.view addSubview:_curAds];
    [self layoutSubviews];
}

- (void)adsMix:(AdsMix *)object didUnloadAdAndRestartRequest:(BaseAds *)ad
{
    [self layoutSubviews];
}

@end
